#include "modulea.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Student students[MAX_STUDENTS];
Course courses[MAX_COURSES];
int studentCount = 0;
int courseCount = 0;
int coursePeople[MAX_COURSES] = {0}; //记录coursePeople[i] 记录选择id为i的课程的人数。

void updateCredits(Student* stu) {
    int sumCredits = 0;
    for (int i = 0; i < stu->courseNum; i++) {
        sumCredits += stu->seletctedCourse[i].credits;
    }
    stu->totalCredits = sumCredits;
}


void addStudent() {
    if (studentCount >= MAX_STUDENTS) {
        printf("Student database is full.\n");
        return;
    }

    Student student;
    printf("Enter student ID: ");
    scanf("%d", &student.id);
    printf("Enter student name: ");
    scanf("%s", student.name);
    student.courseNum = 0;
    int num;
    printf("输入要选课的个数：\n");
    scanf("%d",&num);
    student.courseNum = num;
    printf("输入该学生选择的课程ID，以回车结束：\n");
    int cid;
    int i = 0;
    while (scanf("%d", &cid) == 1) {
        for (int j = 0; j < MAX_COURSES; j++) {
            if (courses[j].id == cid) {
                memcpy(&student.seletctedCourse[i++], &courses[j], sizeof(Course));
                coursePeople[cid]++;
            }
        }
        num--;
        if (num == 0) break;
    }

    updateCredits(&student);
    students[studentCount++] = student;

    printf("Student added successfully.\n");
}


void modifyStudent() {
    int id;
    char name[MAX_NAME_LENGTH];
    printf("Enter student ID: ");
    scanf("%d", &id);

    int i;
    for (i = 0; i < studentCount; i++) {
        if (students[i].id == id || strcmp(students[i].name, name) == 0) {
            printf("Enter new name: ");
            scanf("%s", students[i].name);
            printf("Student modified successfully.\n");
            return;
        }
    }

    printf("Student not found.\n");
}

void deleteStudent() {
    int id;
    char name[MAX_NAME_LENGTH];
    printf("Enter student ID: ");
    scanf("%d", &id);

    int i;
    for (i = 0; i < studentCount; i++) {
        if (students[i].id == id || strcmp(students[i].name, name) == 0) {
            int j;
            for (int k = 0; k < students[i].courseNum; k++) {
                int deleteCourseId = students[i].seletctedCourse[k].id;
                coursePeople[deleteCourseId]--;
            }
            for (j = i; j < studentCount - 1; j++) {
                students[j] = students[j + 1];
            }
            studentCount--;
            printf("Student deleted successfully.\n");
            return;
        }
    }
    updateSelectedCourse(courses);
    printf("Student not found.\n");
}

void viewStudentDetails() {
    int id;
    char name[MAX_NAME_LENGTH];
    printf("Enter student ID: ");
    scanf("%d", &id);

    int i;
    for (i = 0; i < studentCount; i++) {
        if (students[i].id == id || strcmp(students[i].name, name) == 0) {
            printf("Student ID: %d\n", students[i].id);
            printf("Student name: %s\n", students[i].name);
            printf("选择的课程为：\n");
            int length = students[i].courseNum;
            for (int j = 0; j < length; j++) {
                printf("选课%d：%s\n", j, students[i].seletctedCourse[j].name);
            }
            printf("Total credits: %d\n", students[i].totalCredits);
            return;
        }
    }
    
    printf("Student not found.\n");
}
